package com.skyline.wo.mapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.skyline.base.type.RemoteDeviceType;
import com.skyline.wo.model.ShortMessage;

public class ShortMessageMapper {

	private static final ShortMessageMapper MAPPER = new ShortMessageMapper();

	// private static BasicDBObject queryConditionById = null;

	// private static BasicDBObject queryConditionByOwnerId = null;
	private static BasicDBObject sortCondition = null;

	private static String ID_COLUMN = "_id";
	private static String OWNER_ID_COLUMN = "oi";
	private static String OWNER_NICKNAME_COLUMN = "on";
	private static String OWNER_PORTRAIT_COLUMN = "op";
	private static String TEXT_COLUMN = "t";
	private static String SOURCE_OWNER_ID_COLUMN = "soi";
	private static String SOURCE_OWNER_NICKNAME_COLUMN = "son";
	private static String COMMENT_COLUMN = "c";
	private static String CREATE_TIME_COLUMN = "ct";
	private static String COMMETTER_IDS_COLUMN = "cis";
	private static String COMMETTER_NICKNAMES_COLUMN = "cns";
	private static String COMMENTS_COLUMN = "cs";
	private static String SPOT_LAT_COLUMN = "la";
	private static String SPOT_LNG_COLUMN = "ln";
	private static String SPOT_ID_COLUMN = "si";
	private static String SPOT_NAME_COLUMN = "sn";
	private static String REMOTE_DEVICE_TYPE_COLUMN = "rd";

	public static ShortMessageMapper getMapper() {
		return MAPPER;
	}

	public static BasicDBObject getQueryConditionByOwnerId(Long ownerId) {
		// if (queryConditionByOwnerId == null) {
		BasicDBObject queryConditionByOwnerId = new BasicDBObject();
		queryConditionByOwnerId.put(OWNER_ID_COLUMN, ownerId);
		// }

		return queryConditionByOwnerId;
	}

	public static BasicDBObject getQueryConditionById(String id) {
		// if(queryConditionById==null){
		BasicDBObject queryConditionById = new BasicDBObject();
		queryConditionById.put(ID_COLUMN, new ObjectId(id));
		// }
		return queryConditionById;
	}

	public static BasicDBObject getSortCondition() {
		if (sortCondition == null) {
			sortCondition = new BasicDBObject();
			sortCondition.put(CREATE_TIME_COLUMN, -1);
		}
		return sortCondition;
	}

	public static List<ShortMessage> map2ShortMessages(DBCursor cursor) {
		if (cursor == null) {
			return null;
		} else {
			List<ShortMessage> ms = new ArrayList<ShortMessage>();
			while (cursor.hasNext()) {
				ms.add(map2ShortMessage(cursor.next()));
			}
			return ms;
		}
	}

	@SuppressWarnings("unchecked")
	public static ShortMessage map2ShortMessage(DBObject o) {
		ShortMessage m = new ShortMessage();
		m.setComment((String) o.get(COMMENT_COLUMN));
		m.setCommetterIds((List<Long>) o.get(COMMETTER_IDS_COLUMN));
		m.setCommetterNicknames((List<String>) o.get(COMMETTER_NICKNAMES_COLUMN));
		m.setComments((List<String>) o.get(COMMENTS_COLUMN));
		m.setCreateTime((Date) o.get(CREATE_TIME_COLUMN));
		m.setId(o.get(ID_COLUMN).toString());
		m.setOwnerId((Long) o.get(OWNER_ID_COLUMN));
		m.setOwnerNickname((String) o.get(OWNER_NICKNAME_COLUMN));
		m.setOwnerPortrait((String) o.get(OWNER_PORTRAIT_COLUMN));
		String remoteDeviceType = (String) o.get(REMOTE_DEVICE_TYPE_COLUMN);
		if (remoteDeviceType != null) {
			m.setRemoteDeviceType(RemoteDeviceType.valueOf(remoteDeviceType));
		}
		m.setSourceOwnerId((Long) o.get(SOURCE_OWNER_ID_COLUMN));
		m.setSourceOwnerNickname((String) o.get(SOURCE_OWNER_NICKNAME_COLUMN));
		m.setSpotId((Long) o.get(SPOT_ID_COLUMN));
		m.setSpotLat((Double) o.get(SPOT_LAT_COLUMN));
		m.setSpotLng((Double) o.get(SPOT_LNG_COLUMN));
		m.setSpotName((String) o.get(SPOT_NAME_COLUMN));
		m.setText((String) o.get(TEXT_COLUMN));
		return m;

	}

	public static BasicDBObject map2DBObject(Long ownerId, String ownerNickname, String ownerPortrait, String text, Long sourceOwnerId,
			String sourceOwnerNickname, String comment, Date createTime, List<Long> commetterIds, List<String> commetterNicknames,
			List<String> comments, Double spotLat, Double spotLng, Long spotId, String spotName, RemoteDeviceType remoteDeviceType) {

		BasicDBObject o = new BasicDBObject();
		if (ownerId != null) {
			o.put(OWNER_ID_COLUMN, ownerId);
		}
		if (ownerNickname != null) {
			o.put(OWNER_NICKNAME_COLUMN, ownerNickname);
		}
		if (ownerPortrait != null) {
			o.put(OWNER_PORTRAIT_COLUMN, ownerPortrait);
		}
		if (text != null) {
			o.put(TEXT_COLUMN, text);
		}
		if (sourceOwnerId != null) {
			o.put(SOURCE_OWNER_ID_COLUMN, sourceOwnerId);
		}
		if (sourceOwnerNickname != null) {
			o.put(SOURCE_OWNER_NICKNAME_COLUMN, sourceOwnerNickname);
		}
		if (comment != null) {
			o.put(COMMENT_COLUMN, comment);
		}
		if (createTime != null) {
			o.put(CREATE_TIME_COLUMN, createTime);
		}
		if (commetterIds != null) {
			o.put(COMMETTER_IDS_COLUMN, commetterIds);
		}
		if (commetterNicknames != null) {
			o.put(COMMETTER_NICKNAMES_COLUMN, commetterNicknames);
		}
		if (comments != null) {
			o.put(COMMENTS_COLUMN, comments);
		}
		if (spotLat != null) {
			o.put(SPOT_LAT_COLUMN, spotLat);
		}
		if (spotLng != null) {
			o.put(SPOT_LNG_COLUMN, spotLng);
		}
		if (spotId != null) {
			o.put(SPOT_ID_COLUMN, spotId);
		}
		if (spotName != null) {
			o.put(SPOT_NAME_COLUMN, spotName);
		}
		if (remoteDeviceType != null) {
			o.put(REMOTE_DEVICE_TYPE_COLUMN, remoteDeviceType.toString());
		}
		return o;

	}

}
